import {reqNewhave, reqwantuan,reqtime,reqgoods} from '../../request/api'
//初始状态
const initState={
        newhav:[],//新人专享
        wanpople:[],//万人团
        onlytime:[],//秒杀
        goods:[],//商品信息
        n:0
}

//TYPES
const TYPES={
        // 新人专享
        CHANGE_NEWHAV:'CHANGE_NEWHAV',
        //万人团
        CHANGE_WANPOPLE:'CHANGE_WANPOPLE',
        //限时秒杀
        CHANGE_ONLYTIME:'CHANGE_ONLYTIME',
        // 商品信息
        CHANGE_GOODS:'CHANGE_GOODS',
        CHANGE_GOODS_N:'CHANGE_GOODS_N'
}

//reducer
const reducer =(state=initState,action)=>{
        switch(action.type){
                case TYPES.CHANGE_NEWHAV:
                        return{
                                ...state,
                                newhav:action.newhav
                        }
                case TYPES.CHANGE_WANPOPLE:
                        return{
                                ...state,
                                wanpople:action.wanpople
                        }
                case TYPES.CHANGE_ONLYTIME:
                        return{
                                ...state,
                                onlytime:action.onlytime
                        }
                case TYPES.CHANGE_GOODS:
                        return{
                                ...state,
                                goods:action.goods
                        }
                case TYPES.CHANGE_GOODS_N:
                        return{
                                ...state,
                                n:action.n
                        }
                default:
                        return state
        }
}

//creators 做逻辑 方法  请求
export const actions={
        changeNewhav:(newhav)=>({type:TYPES.CHANGE_NEWHAV,newhav}),
        //请求新人专享
        requestNewhav:()=>(dispatch,getState)=>{
                reqNewhave().then(res=>{
                        if(res.data.code==200){
                                dispatch(actions.changeNewhav(res.data.list))
                        }
                        
                })
        },

        //万人团
        changeWanpople:(wanpople)=>({type:TYPES.CHANGE_WANPOPLE,wanpople}),
        //万人团请求
        requestWanpople:()=>(dispatch,getState)=>{
                reqwantuan().then(res=>{
                        if(res.data.code==200){
                            dispatch(actions.changeWanpople(res.data.list))    
                        }
                })
        },

        // 限时秒杀
        changeOnlytime:(onlytime)=>({type:TYPES.CHANGE_ONLYTIME,onlytime}),
        // 请求限时秒杀
        requestOnlytime:()=>(dispatch,getState)=>{
                reqtime().then(res=>{
                        if(res.data.code==200){
                                dispatch(actions.changeOnlytime(res.data.list))
                        }
                })
        },

        // 商品信息
        changeGoods:(goods)=>({type:TYPES.CHANGE_GOODS,goods}),
        // 请求额商品信息
        requestGoods:()=>(dispatch,getState)=>{
                reqgoods().then(res=>{
                        if(res.data.code==200){
                                dispatch(actions.changeGoods(res.data.list))
                        }
                })
        },
        changeN:n=>({type:TYPES.CHANGE_GOODS_N,n})
}

// 导出数据给组件
// 新人专享
export const getNewhav=(state)=>state.home.newhav
//万人团
export const getWanpople=(state)=>state.home.wanpople
//限时秒杀
export const getOnlyTime=(state)=>state.home.onlytime
//商品信息
export const getGoods=(state)=>state.home.goods 

//显示到页面的数据
export const showGoods=state=>{
        let goods=getGoods(state);//获取所有商品信息
        let n=getN(state)//点击获取n==下标
        return goods.length===0?[]:goods[n].content;
}

export const getN=state=>state.home.n
// 导出reducer
export default reducer